
var BattleUnitAim = cc.Node.extend({

    TIME : 1.5,           // 总瞄准时间

    // 评价分成  miss bad good perfect
    // perfect为1, 正负0.1
    // good为正负0.25 (正负45°)
    // good为正负0.5 (正负90°)
    PERFECT_TIME_1 : 0.95,
    PERFECT_TIME_2 : 1.05,
    GOOD_TIME_1 : 0.75,
    GOOD_TIME_2 : 1.25,
    BAD_TIME_1 : 0.5,
    BAD_TIME_2 : 1.5,

    imgBg : null,
    imgUp : null,
    imgDown : null,

    _cnt : 0.0,
    _atkGrade : helper.ATTACK_GRADE_MISS,   // 记录上一次的成绩

    ctor : function () {
		this._super();
        
        var sp = cc.Sprite.create('res/battle/aim_0.png');
        //sp.setOpacity(130);
        this.imgBg = sp;
        this.addChild(this.imgBg);
        
        var a = 100;
        var color = cc.c4b(0,200,0,a)
        sp = cc.Sprite.create('res/battle/aim_1.png');
        sp.setOpacity(a);
        //sp.setColor(color);
        this.imgUp = sp;
        this.addChild(this.imgUp);

        sp = cc.Sprite.create('res/battle/aim_2.png');
        sp.setOpacity(a);
        //sp.setColor(color);
        this.imgDown = sp;
        this.addChild(this.imgDown);


        //* test
        //this.run();
        //this.aniHide();
	},

    run : function () {
        this._cnt = 0.0;
        this.imgDown.setRotation(0);

        this.imgBg.setOpacity(130);
        this.imgUp.setOpacity(200);
        this.imgDown.setOpacity(200);

        this.schedule(this.update, -1);
    },

    stop : function () {
        this.unschedule(this.update);
        // 消失
        this.aniHide();

        // 计算才成绩
        this._atkGrade = helper.ATTACK_GRADE_MISS;
        if (this._cnt >= this.PERFECT_TIME_1 && this._cnt <= this.PERFECT_TIME_2) {
            this._atkGrade = helper.ATTACK_GRADE_PERFECT;
        }
        else if (this._cnt >= this.GOOD_TIME_1 && this._cnt <= this.GOOD_TIME_2) {
            this._atkGrade = helper.ATTACK_GRADE_GOOD;
        }
        else if (this._cnt >= this.BAD_TIME_1 && this._cnt <= this.BAD_TIME_2) {
            this._atkGrade = helper.ATTACK_GRADE_BAD;
        }
    },

    getAttackGrade : function () {
        return this._atkGrade;
    },

    update : function (dt) {
        this._cnt += dt;
        if(this._cnt > this.TIME) {
            this.stop();
            return;
        }

        var p = this._cnt / this.TIME;
        if (p > 1) p = 1;
        this.imgDown.setRotation( -270 * p );
    },


    aniHide : function () {
        var time = 1;
        var fade = cc.FadeTo.create(time, 0);                  // 渐变
        var fade1 = cc.FadeTo.create(time, 0);
        var fade2 = cc.FadeTo.create(time, 0);
        
        this.imgBg.runAction(fade);
        this.imgUp.runAction(fade1);
        this.imgDown.runAction(fade2);
    },
});

